<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chapter 4. Boost.Any 1.2</title>
<link rel="stylesheet" href="../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="libraries.html" title="Part I. The Boost C++ Libraries (BoostBook Subset)">
<link rel="prev" href="align/history.html" title="History">
<link rel="next" href="any/examples.html" title="Examples">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../boost.png"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="align/history.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="any/examples.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="chapter">
<div class="titlepage"><div>
<div><h2 class="title">
<a name="any"></a>Chapter 4. Boost.Any 1.2</h2></div>
<div><p class="copyright">Copyright © 2001 Kevlin Henney</p></div>
<div><p class="copyright">Copyright © 2013-2025 Antony Polukhin</p></div>
<div><div class="legalnotice">
<a name="any.legal"></a><p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></div>
</div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl class="toc">
<dt><span class="section"><a href="any.html#any.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="any/examples.html">Examples</a></span></dt>
<dt><span class="section"><a href="any/ValueType.html"><span class="emphasis"><em>ValueType</em></span> requirements</a></span></dt>
<dt><span class="section"><a href="reference_section_of_boost_any.html">Reference Section of Boost.Any</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="reference_section_of_boost_any.html#doxygen.reference_section_of_boost_any.any_8hpp">Header &lt;boost/any.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="reference_section_of_boost_any.html#doxygen.reference_section_of_boost_any.bad__any__cast_8hpp">Header &lt;boost/any/bad_any_cast.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="reference_section_of_boost_any.html#doxygen.reference_section_of_boost_any.basic__any_8hpp">Header &lt;boost/any/basic_any.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="reference_section_of_boost_any.html#doxygen.reference_section_of_boost_any.fwd_8hpp">Header &lt;boost/any/fwd.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="reference_section_of_boost_any.html#doxygen.reference_section_of_boost_any.unique__any_8hpp">Header &lt;boost/any/unique_any.hpp&gt;</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="any/acknowledgements.html">Acknowledgements</a></span></dt>
</dl>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="any.introduction"></a><a class="link" href="any.html#any.introduction" title="Introduction">Introduction</a>
</h2></div></div></div>
<p>
      There are times when a generic (in the sense of <span class="emphasis"><em>general</em></span>
      as opposed to <span class="emphasis"><em>template-based programming</em></span>) type is needed:
      variables that are truly variable, accommodating values of many other more
      specific types rather than C++'s normal strict and static types. We can distinguish
      three basic kinds of generic type:
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          Converting types that can hold one of a number of possible value types,
          e.g. <code class="computeroutput"><span class="keyword">int</span></code> and <code class="computeroutput"><span class="identifier">string</span></code>, and freely convert between them,
          for instance interpreting <code class="computeroutput"><span class="number">5</span></code>
          as <code class="computeroutput"><span class="string">"5"</span></code> or vice-versa.
          Such types are common in scripting and other interpreted languages. <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span></code> supports such conversion
          functionality.
        </li>
<li class="listitem">
          Discriminated types that contain values of different types but do not attempt
          conversion between them, i.e. <code class="computeroutput"><span class="number">5</span></code>
          is held strictly as an <code class="computeroutput"><span class="keyword">int</span></code>
          and is not implicitly convertible either to <code class="computeroutput"><span class="string">"5"</span></code>
          or to <code class="computeroutput"><span class="number">5.0</span></code>. Their indifference
          to interpretation but awareness of type effectively makes them safe, generic
          containers of single values, with no scope for surprises from ambiguous
          conversions.
        </li>
<li class="listitem">
          Indiscriminate types that can refer to anything but are oblivious to the
          actual underlying type, entrusting all forms of access and interpretation
          to the programmer. This niche is dominated by <code class="computeroutput"><span class="keyword">void</span>
          <span class="special">*</span></code>, which offers plenty of scope
          for surprising, undefined behavior.
        </li>
</ul></div>
<p>
      The <code class="computeroutput"><a class="link" href="doxygen/reference_section_of_boost_any/classboost_1_1any.html" title="Class any">boost::any</a></code> class (based on the
      class of the same name described in <a href="http://www.two-sdg.demon.co.uk/curbralan/papers/ValuedConversions.pdf" target="_top">Valued
      Conversions</a> by Kevlin Henney, <span class="emphasis"><em>C++ Report</em></span> 12(7),
      July/August 2000) is a variant value type based on the second category. It
      supports copying of any value type and safe checked extraction of that value
      strictly against its type. A similar design, offering more appropriate operators,
      can be used for a generalized function adaptor, <code class="computeroutput"><span class="identifier">any_function</span></code>,
      a generalized iterator adaptor, <code class="computeroutput"><span class="identifier">any_iterator</span></code>,
      and other object types that need uniform runtime treatment but support only
      compile-time template parameter conformance.
    </p>
<p>
      The <code class="computeroutput"><a class="link" href="doxygen/reference_section_of_boost_any/classboost_1_1anys_1_1unique__any.html" title="Class unique_any">boost::anys::unique_any</a></code>
      class (based on the <a href="https://userver.tech/d3/d49/classutils_1_1AnyMovable.html" target="_top">utils::AnyMovable</a>
      class from the <a href="https://userver.tech/" target="_top">🐙 userver framework</a>)
      is a variant value type based on the second category. It supports safe checked
      extraction of that value strictly against its type and passing ownership of
      the value. Think of <code class="computeroutput"><a class="link" href="doxygen/reference_section_of_boost_any/classboost_1_1anys_1_1unique__any.html" title="Class unique_any">boost::anys::unique_any</a></code>
      as of an alternative to <code class="computeroutput"><a class="link" href="doxygen/reference_section_of_boost_any/classboost_1_1any.html" title="Class any">boost::any</a></code>
      (or to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">any</span></code>) that does not require copy or move construction
      from the held type.
    </p>
</div>
</div>
<div class="copyright-footer"></div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="align/history.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="any/examples.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
